<?php
/**
 * Created by PhpStorm.
 * User: tom_xia
 * Date: 2018/6/4
 * Time: 13:21
 * pv统计入库
 */
require_once dirname(dirname(__DIR__)) . "/common.php";
use app\lib\db\DbException;
use app\cls\tom\Queue;
use app\cls\tom\SysResource;
use app\cls\tom\Process;

$process = new Process(__FILE__);
$process->setProcessNum(1);
$process->initProcess();

$redis = SysResource::getRedis();
$db =  SysResource::getDB(true);

try{
    while(true){
        for($i = 1 ;$i<=5;$i++){
            for($j = 0 ;$j < 100;$j++){
                //test为测试，no_test为正式
                $PATH="no_test";
                if($PATH=="test"){
                    $key = REDIS_PREFIX."test_material_statics_".$i;
                    $key1 = REDIS_PREFIX."test_material_statics_1";
                    $key2 = REDIS_PREFIX."test_material_statics_2";
                    $key3 = REDIS_PREFIX."test_material_statics_3";
                    $key4 = REDIS_PREFIX."test_material_statics_4";
                    $key5 = REDIS_PREFIX."test_material_statics_5";
                }else if($PATH=="no_test"){
                    $key = REDIS_PREFIX."material_statics_".$i;
                    $key1 = REDIS_PREFIX."material_statics_1";
                    $key2 = REDIS_PREFIX."material_statics_2";
                    $key3 = REDIS_PREFIX."material_statics_3";
                    $key4 = REDIS_PREFIX."material_statics_4";
                    $key5 = REDIS_PREFIX."material_statics_5";
                }
                $dataStr = $redis->rPop($key);
                $result =  json_decode($dataStr,true);

                if($result){
                    //                $db->select("count_material_share");
//                $db->from("video");
//                $db->where(['video_id'=>$result['id'],'video_uuid'=>$result['uniqueid']]);
//                $query =  $db->get();
//                $datas = $query->one();
//                //如果分享数为0 或者分享数为空
//                if(empty($datas['count_material_share']) || $datas['count_material_share']=="0"){
//                    //就去点赞表查询是否有这个数据被点赞了
//                    $db->select("log_id");
//                    $db->from("data_logs_2018");
//                    $db->where(['log_uniqueid'=>$result['uniqueid'],"log_operate"=>"share","material_id"=>$result['id']]);
//                    $query =  $db->get();
////                        echo $db->last_query();exit;
//                    $datas1 = $query->all();
//                    $datas=count($datas1);
//                    //修改喜欢数
//                    if(empty($datas)){
//                        $re=$db->update("video",['count_material_share'=>"!count_material_share+1"],['video_uuid'=>$result['uniqueid'],"video_id"=>$result['id']]);
//                    }else{
//                        $re=$db->update("video",['count_material_share'=>$datas+1],['video_uuid'=>$result['uniqueid'],"video_id"=>$result['id']]);
//                    }
//
//                }else{
//                    //修改喜欢数
//                    $re=$db->update("video",['count_material_share'=>"!count_material_share+1"],['video_uuid'=>$result['uniqueid'],"video_id"=>$result['id']]);
//                }
                    $re=$db->update("video",['count_material_share'=>"!count_material_share+1"],['video_uuid'=>$result['uniqueid'],"video_id"=>$result['id']]);
                    if(!$re){
                        if($i==1){
                            $redis->lPush($key2,$dataStr);
                        }else if($i==2){
                            $redis->lPush($key3,$dataStr);
                        }else if($i==3){
                            $redis->lPush($key4,$dataStr);
                        }else if($i==4){
                            $redis->lPush($key5,$dataStr);
                        }else if($i==5){
                            $redis->lPush($key1,$dataStr);
                        }
                    }
                }else{
                    continue;
                }
            }
        }
        sleep(1);
        $process->checkProcessTime(true);
    }
}catch (Exception $e){
    $process->log($e->getMessage());
}
